package com.android.allwinner.newaw360.utils;

import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by Administrator on 2016/10/31.
 */

public class FileOrderUtils {
    private static final String TAG = FileOrderUtils.class.getSimpleName();
    //按照文件大小排序
    public static List<File> orderByLength(String fliePath) {
        List<File> files = Arrays.asList(new File(fliePath).listFiles());
        Collections.sort(files, new Comparator<File>() {
            public int compare(File f1, File f2) {
                long diff = f1.length() - f2.length();
                if (diff > 0)
                    return 1;
                else if (diff == 0)
                    return 0;
                else
                    return -1;
            }

            public boolean equals(Object obj) {
                return true;
            }
        });
        return files;
    }

    //按照文件名称排序
    public static List<File> orderByName(String fliePath) {
        List<File> files = Arrays.asList(new File(fliePath).listFiles());
        Collections.sort(files, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1.isDirectory() && o2.isFile())
                    return -1;
                if (o1.isFile() && o2.isDirectory())
                    return 1;
                return o1.getName().compareTo(o2.getName());
            }
        });
        return files;
    }

    //按日期排序
    public static List<String> orderByDate(String[] filePath) {
        List<String> fileList = Arrays.asList(filePath);

        if (fileList != null) {
            Collections.sort(fileList, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    String time1 = o1.split("_", 2)[1];
                    String time2 = o2.split("_", 2)[1];
                    return time1.compareTo(time2);
                }
            });
        }
        return fileList;
    }
}
